package com.xunshangwang.pinhaohuo.decorations;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xunshangwang.pinhaohuo.R;

/**
 * Created by xzp on 16/1/27.
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration{
    private Drawable mDivider;
    public DividerItemDecoration(Context context) {
        mDivider=ContextCompat.getDrawable(context,R.drawable.default_divider);
    }
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int childCount=parent.getChildCount();
        for(int i=0;i<childCount;i++){
            if(canDraw(i)){
                View child=parent.getChildAt(i);
                mDivider.setBounds(child.getLeft(),child.getBottom(),child.getRight(),child.getBottom()+mDivider.getIntrinsicHeight());
                mDivider.draw(c);
            }
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position=parent.getChildAdapterPosition(view);
        if(canDraw(position))
            outRect.set(0,0,0,mDivider.getIntrinsicHeight());

    }
    protected boolean canDraw(int position){

        return true;
    }
}
